const { Controller } = require('egg');
const validate = require('../rule/rule');

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }


  /**
   * @param req
   * @param res
   * @api {GET} /dictionary/list 获取字典列表
   * @apiName list
   * @apiGroup dictionary
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiQuery  { String } parent_id 父id
   * @apiQuery  { Number } pageIndex 页码
   * @apiQuery  { Number } pageSize 页面容量
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async list(req, res) {
    const ctx = this.ctx;
    try {
      const { query } = req;
      const { parent_id, pageIndex, pageSize } = await validate.listValidate(query);

      const data = await ctx.service.dictionary.list(parent_id, pageIndex, pageSize);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @param req
   * @param res
   * @api {GET} /dictionary/tree 获取字典树
   * @apiName list
   * @apiGroup dictionary
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async tree(req, res) {
    const ctx = this.ctx;
    try {
      const data = await ctx.service.dictionary.tree();
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @param req
   * @param res
   * @api {POST} /dictionary/insert 新增
   * @apiName insert
   * @apiGroup dictionary
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } name 名称
   * @apiBody  { String } key key
   * @apiBody  { String } value 值
   * @apiBody  { String } comment 注释
   * @apiBody  { String } parent_id 父id
   * @apiBody  { String } is_enable 是否开启
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async insert(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { name, key, value, comment, parent_id, is_enable } = await validate.insertValidate(body);

      const data = await ctx.service.dictionary.insert(name, key, value, comment, parent_id, is_enable);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @param req
   * @param res
   * @api {POST} /dictionary/update 更新
   * @apiName update
   * @apiGroup dictionary
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } name 名称
   * @apiBody  { String } key key
   * @apiBody  { String } value 值
   * @apiBody  { String } comment 注释
   * @apiBody  { String } uuid uuid
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async update(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { uuid, name, key, value, comment } = await validate.updateValidate(body);

      const data = await ctx.service.dictionary.update(uuid, name, key, value, comment);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @param req
   * @param res
   * @api {POST} /dictionary/enable 开启关闭
   * @apiName enable
   * @apiGroup dictionary
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } is_enable 是否开启
   * @apiBody  { String } uuid uuid
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async enable(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { uuid, is_enable } = await validate.enableValidate(body);

      const data = await ctx.service.dictionary.enable(uuid, is_enable);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @param req
   * @param res
   * @api {POST} /dictionary/move 移动
   * @apiName move
   * @apiGroup dictionary
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } is_parent 父id
   * @apiBody  { String } uuid uuid
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async move(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { uuid, parent_id } = await validate.moveValidate(body);

      const data = await ctx.service.dictionary.move(uuid, parent_id);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @param req
   * @param res
   * @api {POST} /dictionary/delete 删除
   * @apiName delete
   * @apiGroup dictionary
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } ids uuid数组
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async delete(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { ids } = await validate.deleteValidate(body);

      const data = await ctx.service.dictionary.delete(ids);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }
}

module.exports = HomeController;
